package com.example.his.api.front.service.impl;

import cn.hutool.core.map.MapUtil;
import com.example.his.api.common.PageUtils;
import com.example.his.api.db.dao.GoodsDao;
import com.example.his.api.front.service.GoodsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author huixh
 */
@Service("FrontGoodsServiceImpl")
public class GoodsServiceImpl implements GoodsService {
    @Resource
    private GoodsDao goodsDao;

    @Override
    public Map<String, Object> searchIndexGoodsByPartId(Integer[] partIds) {
        Map<String, Object> result = new HashMap<>();
        for (Integer partId : partIds) {
            result.put(partId.toString(), goodsDao.searchByPartIdLimit4(partId));
        }
        return result;
    }

    @Override
    public PageUtils searchListByPage(Map<String, Object> params) {

        List<HashMap<String, Object>> list = new ArrayList<>();
        long count = goodsDao.searchListCount(params);
        if (count > 0) {
            list = goodsDao.searchListByPage(params);
        }
        int page = MapUtil.getInt(params, "page");
        int length = MapUtil.getInt(params, "length");

        return new PageUtils(count, length, page, list);
    }
}
